﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace Adams.Public.Api
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Membership",
                "Web/Membership/{controller}/{action}/{id}",
                new { controller = "", action = "", id = UrlParameter.Optional },
                new string[] { "Adams.Public.Api.Controllers.Web.Areas.Membership" }
            );

            routes.MapRoute(
                "Web",
                "Web/{controller}/{action}/{id}",
                new { controller = "Home", action = "PageLoad", id = UrlParameter.Optional },
                new string[] { "Adams.Public.Api.Controllers.Web" }
            );

            routes.MapRoute(
                "Mobile",
                "Mobile/{controller}/{action}/{id}",
                new { controller = "Home", action = "PageLoad", id = UrlParameter.Optional },
                new string[] { "Adams.Public.Api.Controllers.Mobile" }
            );

            routes.MapRoute(
                "Default",
                "{controller}/{action}/{id}",
                new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                new string[] { "Adams.Public.Api.Controllers" }
            );

        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

            HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            HttpContext.Current.Response.Cache.SetNoStore();

            //string ret = string.Empty;
            //using (System.IO.Stream stream = HttpContext.Current.Request.InputStream)
            //{
            //    using (System.IO.StreamReader reader = new System.IO.StreamReader(stream))
            //    {
            //        ret = reader.ReadToEnd();
            //        reader.Close();
            //    }
            //    stream.Close();
            //}
            

            EnableCrossDmainAjaxCall();
        }

        private void EnableCrossDmainAjaxCall()
        {
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");

            if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
            {
                HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
                HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
                HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
                HttpContext.Current.Response.End();
            }
        }
    }
}